package com.usefullc.crawler.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.enums.ValidActionEnum;
import com.usefullc.crawler.param.ProcessOperateParam;
import com.usefullc.crawler.selenium.CrawlerWebDriverBuilder3;
import com.usefullc.crawler.vo.ClientResultVo;
import com.usefullc.system.validator.UsefulPropertyVerify;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.crawler.domain.ActiveProcess;
import com.usefullc.crawler.param.ActiveProcessParam;
import com.usefullc.crawler.vo.ActiveProcessVo;
import com.usefullc.crawler.query.ActiveProcessQuery;
import com.usefullc.crawler.service.ActiveProcessService;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 爬虫活动流程控制类
 * @author: 星空
 * @date: 2022-12-14
 */
@RestController
@RequestMapping("/crawler/activeProcess/")
@Api( tags="爬虫活动流程")
public class ActiveProcessController extends BaseController {

    @Autowired
    private ActiveProcessService activeProcessService;

    @Autowired
    private CrawlerWebDriverBuilder3 crawlerWebDriverBuilder3;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫活动流程列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) ActiveProcessQuery query) {
        Pager<ActiveProcessVo> page = activeProcessService.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫活动流程所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(@UsefulValid(query = true) ActiveProcessQuery query) {
        List<ActiveProcess> dataList = activeProcessService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫活动流程详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        ActiveProcess activeProcess = activeProcessService.findById(id);
        return WebResultBuilder.successResult(activeProcess);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "创建爬虫活动流程", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody ActiveProcessParam param) {
        ActiveProcess activeProcess = BeanUtils.beanCopy(param, ActiveProcess.class);
        return successResult(activeProcessService.save(activeProcess));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改爬虫活动流程", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody ActiveProcessParam param) {
        ActiveProcess oldBean = activeProcessService.findById(param.getId());
        BeanUtils.beanCopy(param, oldBean);
        return successResult(activeProcessService.update(oldBean));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除爬虫活动流程",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        activeProcessService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除爬虫活动流程",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        activeProcessService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "活动流程开始", response = WebResult.class)
    @PostMapping(path = "start", produces = "application/json")
    public WebResult start(@RequestBody ProcessOperateParam param, HttpServletRequest request) {
        Long templateId = param.getTemplateId();
        ClientResultVo clientResultVo = activeProcessService.start(templateId);
        return WebResultBuilder.successResult(clientResultVo);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "活动流程重新开始(暂时不用)", response = WebResult.class)
    @PostMapping(path = "restart", produces = "application/json")
    public WebResult restart(@RequestBody ProcessOperateParam param, HttpServletRequest request) {
        Long activeProcessId = param.getActiveProcessId();
        activeProcessService.restart(activeProcessId);
        return WebResultBuilder.successResult();
    }
    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "活动流程暂停", response = WebResult.class)
    @PostMapping(path = "pause", produces = "application/json")
    public WebResult pause(@RequestBody ProcessOperateParam param, HttpServletRequest request) {
        Long activeProcessId = param.getActiveProcessId();
        activeProcessService.pause(activeProcessId);
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "活动流程恢复", response = WebResult.class)
    @PostMapping(path = "resume", produces = "application/json")
    public WebResult resume(@RequestBody ProcessOperateParam param, HttpServletRequest request) {
        Long activeProcessId = param.getActiveProcessId();
        activeProcessService.resume(activeProcessId);
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "活动流程关闭", response = WebResult.class)
    @PostMapping(path = "close", produces = "application/json")
    public WebResult close(@RequestBody ProcessOperateParam param, HttpServletRequest request) {
        Long activeProcessId = param.getActiveProcessId();
        activeProcessService.close(activeProcessId);
        return WebResultBuilder.successResult();
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "绑定新的代理", response = WebResult.class)
    @PostMapping(path = "newProxy", produces = "application/json")
    public WebResult newProxy( HttpServletRequest request) {
        //先拦截掉，不让webdriver 继续进行
        activeProcessService.setAtoIntercept(false);

        crawlerWebDriverBuilder3.init();

        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "设置拦截状态", response = WebResult.class)
    @PostMapping(path = "setIntercept", produces = "application/json")
    public WebResult setIntercept( @RequestBody ProcessOperateParam param,HttpServletRequest request) {
        Boolean intercept = param.getIntercept();
        //先拦截掉，不让webdriver 继续进行
        activeProcessService.setAtoIntercept(intercept);

        return WebResultBuilder.successResult(intercept);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "检索出任务到任务队列", response = WebResult.class)
    @PostMapping(path = "retrieveTaskToQueue", produces = "application/json")
    public WebResult retrieveTaskToQueue(@RequestBody ProcessOperateParam param, HttpServletRequest request) {
        Long activeProcessId = param.getActiveProcessId();
        int taskSize = activeProcessService.retrieveTaskToQueue(activeProcessId);
        return WebResultBuilder.successResult("检索出"+taskSize+"个任务到队列",taskSize);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "清空运行中的队列", response = WebResult.class)
    @PostMapping(path = "runningTaskClear", produces = "application/json")
    public WebResult runningTaskClear(@RequestBody ProcessOperateParam param, HttpServletRequest request) {
        Long activeProcessId = param.getActiveProcessId();
        int taskSize = activeProcessService.runningTaskClear(activeProcessId);
        return WebResultBuilder.successResult("清空了"+taskSize+"个运行中的任务",taskSize);
    }






}
